package faw.car.ccentersdk.prop;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zhuima
 */
public class MultiRequestBody implements Parcelable {

    private HashMap<Integer, HQCarPropertyExtParameter> dataList;

    // support key
    private HashMap<String, HQCarPropertyExtParameter> keyDataList;

    public HashMap<Integer, HQCarPropertyExtParameter> getDataList() {
        return dataList;
    }

    public HashMap<String, HQCarPropertyExtParameter> getKeyDataList() { return keyDataList; }

    public void setDataList(HashMap<Integer, HQCarPropertyExtParameter> dataList) {
        this.dataList = dataList;
    }

    public void setKeyDataList(HashMap<String, HQCarPropertyExtParameter> keyDataList) {
        this.keyDataList = keyDataList;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if (dataList != null && !dataList.isEmpty()) {
            dest.writeInt(dataList.size());
            for (Map.Entry<Integer, HQCarPropertyExtParameter> entry : dataList.entrySet()) {
                dest.writeInt(entry.getKey());
                if (entry.getValue() != null) {
                    dest.writeParcelable(entry.getValue(), flags);
                } else {
                    dest.writeParcelable(new HQCarPropertyExtParameter(), flags);
                }
            }
        }

        if (keyDataList != null && !keyDataList.isEmpty()) {
            dest.writeInt(keyDataList.size());
            for (Map.Entry<String, HQCarPropertyExtParameter> entry : keyDataList.entrySet()) {
                dest.writeString(entry.getKey());
                if (entry.getValue() != null) {
                    dest.writeParcelable(entry.getValue(), flags);
                } else {
                    dest.writeParcelable(new HQCarPropertyExtParameter(), flags);
                }
            }
        }
    }

    public MultiRequestBody() {
    }

    protected MultiRequestBody(Parcel in) {
        int size = in.readInt();
        dataList = new HashMap<>();
        for (int i = 0; i < size; i++) {
            int id = in.readInt();
            HQCarPropertyExtParameter parameter = in.readParcelable(HQCarPropertyExtParameter.class.getClassLoader());
            dataList.put(id, parameter);
        }
        size = in.readInt();
        keyDataList = new HashMap<>();
        for (int i = 0; i < size; i++) {
            String key = in.readString();
            HQCarPropertyExtParameter parameter = in.readParcelable(HQCarPropertyExtParameter.class.getClassLoader());
            keyDataList.put(key, parameter);
        }
    }

    public static final Creator<MultiRequestBody> CREATOR = new Creator<MultiRequestBody>() {
        @Override
        public MultiRequestBody createFromParcel(Parcel source) {
            return new MultiRequestBody(source);
        }

        @Override
        public MultiRequestBody[] newArray(int size) {
            return new MultiRequestBody[size];
        }
    };
}
